Integrate Recurring Stripe Subscription Payment with PHP

stripe-recurring-payment-in-php

The Stripe Subscription API is a simple way to integrate recurring website payments For recurring billing, subscription payment is required if you want to implement the membership subscription system on the web application. The Stripe payment gateway allows you to integrate recurring payment with the APIs. Stripe subscription is a fast and efficient way for you to buy membership online with a credit card.

In the Stripe subscription payment, the buyer is recurringly charged on the basis of a specific period of time. Your website members will agree to a plan and make the payment without leaving the website with their credit/debit card. We will show you how to integrate Stripe subscription payment with PHP in this tutorial.

Take a look at the directory layout before you begin implementing Stripe Subscription Payment API in PHP.

Generate Stripe Test API Keys

You need the test API keys data to check the subscription payment process.

  • Login to your Stripe account and navigate to the Developers » API keys page.
  • In the TEST DATA block, you’ll see the API keys (Publishable key and Secret key) are listed under the Standard keys section. To show the Secret key, click on the Reveal test key token button.

Copay and Paste the Publishable key and Secret key in your Notepad to use later in the script.

Download Stripe PHP Library

The Stripe PHP library is used to access the PHP Stripe API. It helps create customer, plan, and subscription to Stipre API. The required library files should be included in our source code.

Create Plans, Stripe API and Database Configuration (dbconfig.php)

Note: Stripe API Secret key and Publishable key will be found in the API Keys Data section of your Stripe account.

Create Database Tables

Two tables are required in the database to store the member and subscription information.

1. The following SQL creates an users table to hold the member’s info in the MySQL database.

2. The following SQL creates a user_subscriptions_details table to hold the subscription info in the MySQL database.

Create Stripe Subscription Form (index.php)

In the above code:

the Stripe.js library that helps securely sending sensitive information (credit card) to Stripe directly from the browser.

The following JavaScript code is used to generate a token with the Stripe.js library.

Process Subscription Payment (payment.php)

In this file, the submitted card details are validated and the subscription plan is activated with Stripe API using PHP.

  • Get the logged-in user ID from the SESSION.
  • Retrieve the token, selected plan, card details, and buyer info from the form fields using the PHP $_POST method.
  • Fetch the plan details (name, price, and term) from the $plans array.
  • Include the Stripe PHP library.
  • Set API Secret key using setApiKey() method of Stripe class.
  • Add the customer using create() method of Stripe Customer API.
    • email – Buyer email
    • source – Stripe token
  • Create a plan using the create() method of Stripe Plan API.
    • product – The product whose pricing the created plan will represent.
      • product.name – Plan name.
    • amount – A positive integer in cents (or 0 for a free plan) which will charge on a recurring basis.
    • currency – Three-letter ISO currency code in lowercase.
    • interval – Specify billing frequency (day, week, month or year).
    • interval_count – The number of intervals between subscription billings. For example, interval=month and interval_count=2 bills every 2 months. Maximum of one year (1 year, 12 months, or 52 weeks) interval allowed.
  • Create a subscription using create() method of Stripe Subscription API.
    • customer – The identifier of the customer to subscribe.
    • items – List of the subscription plans.
      • items.plan – Attach plan ID.
  • If the plan and subscription creation are successful, retrieve transaction data. On successful activation,
    • Insert the subscription information into the database.
    • Attach the subscription reference ID to the respective member.
  • Display subscription payment status on the webpage.

Test Card Details

  • 4242424242424242 – Visa
  • 4000056655665556 – Visa (debit)
  • 5555555555554444 – Mastercard
  • 5200828282828210 – Mastercard (debit)
  • 378282246310005 – American Express
  • 6011111111111117 – Discover

Expiration date : 10/2021

CVC number : 123 (Any random number)

Make Stripe Payment Gateway Live

Follow the steps below to make Stripe payment gateway live once the subscription API integration is complete and the payment process works properly.

  • Login to your Stripe account and navigate to the Developers » API keys page.
  • Collect the API keys (Publishable key and Secret key) from the LIVE DATA section.
  • In the dbconfig.php file, replace the Test API keys (Publishable key and Secret key) by the Live API keys (Publishable key and Secret key).

SEE ALSO: How to Integrate 2Checkout Payment Gateway in PHP

SEE ALSO: How to Integrate Paypal Payment System with Paypal API in PHP and MySql

Conclusion

Stripe subscription payment API is the simplest way to accept online subscription credit payment. Using Stripe API and PHP, you can implement the recurring billing featured in the web application. Our example script allows you to periodically charge the user via Stripe at a specific interval. This Stripe payment script’s functionality can be enhanced as per your needs.

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request

Related posts